using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Move3Boll : MonoBehaviour
{
    // Start is called before the first frame update
    //模拟质量
    public float w;
    //用list来储存其他小球的质量
    private List<float> wList = new List<float>();
    //用list来储存其他小球与该小球之间的距离向量
    private List<Vector3> vList = new List<Vector3>();
    //大约为小球的运动速度大小，越大则小球加速越快
    private float speed = 1f;
    //组件刚体
    //private Rigidbody rigidbody;
    // private int []arr = {-1,1};
    void Start()
    {

        //获得小球刚体
        var rigidbody =gameObject.GetComponent<Rigidbody>();
        //一开始小球需要一个扰动速度，否则三个小球会之间汇聚在它们三个质心的位置
        //不会有相互围绕旋转的效果
        rigidbody.velocity = (gameObject.transform.position.normalized + new Vector3(Random.value, Random.value, Random.value));

    }
    // Update is called once per frame
    void Update()
    {
        //获得该标签下的所有游戏物体
        var guids = GameObject.FindGameObjectsWithTag("celestialBodies");
        //guids是个游戏物体数组
        foreach (var guid in guids)
        {
            if (guid == this.gameObject)
            {
                continue;
                //不要把自己这个小球算进去
                //算进去当然也没什么关系
            }
            wList.Add(guid.GetComponent<Move3Boll>().w);
            //加到数组里
            vList.Add(guid.GetComponent<Transform>().position - this.transform.position);

        }
        var rigidbody = gameObject.GetComponent<Rigidbody>();
        for (int i = 0; i < wList.Count; i++)
        {
            //直接给本小球增加一个面向其他小球的力
            rigidbody.AddForce(vList[i] * speed * wList[i]);
            // v -= (consttemp * wList[i])*(vList[i]-transform.position)/(0.0001f+Mathf.Pow(Vector3.Distance(transform.position,vList[i]),3.0f));

        }
        //清空数组
        wList.Clear();
        vList.Clear();
    }
}

